package logic

import (
	"context"
	"gozeroSingle/internal/model"
	"gozeroSingle/internal/svc"
	"gozeroSingle/internal/types"
	"gozeroSingle/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

/*
 +----------------------------------------------------------------------
 + Title        : BannerLogic
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-08-08
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 横幅逻辑
 +----------------------------------------------------------------------
*/

type BannerLogic struct {
	BaseLogic
}

/**
 * 创建横幅逻辑实例
 *
 * @param context.Context ctx ----------------------必传
 * @param *svc.ServiceContext svcCtx ---------------必传
 * @return *BannerLogic
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func NewBannerLogicInstance(ctx context.Context, svcCtx *svc.ServiceContext) *BannerLogic {
	return &BannerLogic{
		BaseLogic{
			Ctx:    ctx,
			SvcCtx: svcCtx,
			Logger: logx.WithContext(ctx),
		},
	}
}

/**
 * 新增
 *
 * @param types.BannerAddRequest params ------------必传
 * @return error
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func (logic *BannerLogic) Add(params types.BannerAddRequest) error {
	_, err := logic.SvcCtx.BannerModel.Insert(logic.Ctx, &model.MallBanner{
		Title:      params.Title,
		ImageUrl:   params.ImageUrl,
		Type:       params.Type,
		Link:       params.Link,
		Note:       params.Note,
		Sort:       params.Sort,
		Status:     1,
		CreateTime: types.Now(),
	})
	if err != nil {
		return err
	}

	return nil
}

/**
 * 删除
 *
 * @param types.BannerDeleteRequest params ---------必传
 * @return error
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func (logic *BannerLogic) Delete(params types.BannerDeleteRequest) error {
	err := logic.SvcCtx.BannerModel.EditByParams(logic.Ctx, map[string]interface{}{
		"status": -2,
	}, map[string]interface{}{
		"id IN (?)": utils.IntSplit(params.Ids, ","),
	})

	if err != nil {
		return err
	}
	return nil
}

/**
 * 编辑
 *
 * @param types.BannerEditRequest params -----------必传
 * @return error
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func (logic *BannerLogic) Edit(params types.BannerEditRequest) error {
	data := map[string]interface{}{}
	if params.Title != "" {
		data["title"] = params.Title
	}
	if params.ImageUrl != "" {
		data["image_url"] = params.ImageUrl
	}
	if params.Type > 0 {
		data["type"] = params.Type
	}
	if params.Link != "" {
		data["link"] = params.Link
	}
	if params.Note != "" {
		data["note"] = params.Note
	}
	if params.Sort > 0 {
		data["sort"] = params.Sort
	}

	return logic.SvcCtx.BannerModel.EditByPrimaryKey(logic.Ctx, data, params.Id)
}

/**
 * 编辑状态
 *
 * @param types.BannerEditStatusRequest params -----必传
 * @return error
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func (logic *BannerLogic) EditStatus(params types.BannerEditStatusRequest) error {
	return logic.SvcCtx.BannerModel.EditByPrimaryKey(logic.Ctx, map[string]interface{}{
		"id":     params.Id,
		"status": params.Status,
	}, params.Id)
}

/**
 * 列表
 *
 * @param types.BannerQueryRequest params ----------必传
 * @param uint8 page -------------------------------必传
 * @param int8 pageSize ----------------------------必传
 * @return []*model.MallBanner, int64, error
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func (logic *BannerLogic) List(params types.BannerQueryRequest, page uint8, pageSize int8) ([]*model.MallBanner, int64, error) {
	data := map[string]interface{}{
		"status <> ?": -2,
	}

	if params.Id > 0 {
		data["id"] = params.Id
	}
	if params.Ids != "" {
		data["id IN (?)"] = utils.IntSplit(params.Ids, ",")
	}
	if params.Title != "" {
		data["title"] = params.Title
	}
	if params.Type > 0 {
		data["type"] = params.Type
	}
	if params.Status != "all" {
		data["status"] = params.Status
	}
	if params.StartCreateTime != "" {
		data["create_time >= ?"] = params.StartCreateTime + " 00:00:00"
	}
	if params.EndCreateTime != "" {
		data["create_time <= ?"] = params.EndCreateTime + " 23:59:59"
	}
	if params.SortField == "" {
		params.SortField = "id"
	}
	if params.SortType == "" {
		params.SortType = "DESC"
	}

	// 获取横幅数量
	total, err := logic.SvcCtx.BannerModel.GetResultsByTotal(logic.Ctx, data)
	if err != nil {
		return nil, 0, err
	}

	// 获取横幅数据
	var banners []*model.MallBanner
	if total > 0 {
		err = logic.SvcCtx.BannerModel.GetResultsByParams(logic.Ctx, &banners, data, uint32(page), pageSize, params.SortField+" "+params.SortType)
		if err != nil {
			return nil, 0, err
		}
	}

	return banners, total, nil
}

/**
 * 数量
 *
 * @param types.BannerQueryRequest params ----------必传
 * @return int64, error
 * @author huwl
 * @date 2025/10/14 12:54:03
 */
func (logic *BannerLogic) Total(params types.BannerQueryRequest) (int64, error) {
	data := map[string]interface{}{
		"status <> ?": -2,
	}

	if params.Id > 0 {
		data["id"] = params.Id
	}
	if params.Ids != "" {
		data["id IN (?)"] = utils.IntSplit(params.Ids, ",")
	}
	if params.Title != "" {
		data["title"] = params.Title
	}
	if params.Type > 0 {
		data["type"] = params.Type
	}
	if params.Status != "all" {
		data["status"] = params.Status
	}
	if params.StartCreateTime != "" {
		data["create_time >= ?"] = params.StartCreateTime + " 00:00:00"
	}
	if params.EndCreateTime != "" {
		data["create_time <= ?"] = params.EndCreateTime + " 23:59:59"
	}

	// 获取横幅数量
	total, err := logic.SvcCtx.BannerModel.GetResultsByTotal(logic.Ctx, data)
	if err != nil {
		return 0, err
	}

	return total, nil
}
